import { Switch, Route, Redirect } from 'react-router-dom'
export const renderRoutes = (routes, authed, extraProps = {}, switchProps = {}) => {
  // routes：静态的路由配置文件
  // authed： 验证路由的一个状态
  // extraProps: 扩展的props,
  // switchProps： 给<Switch>组件的属性；

  return routes ? (
    <Switch {...switchProps}>
      {
        routes.map((routeItem, i) => (
          <Route
            key={routeItem.key || i}
            path={routeItem.path}
            exact={routeItem.exact}
            strict={routeItem.strict}
            render={
              (props) => {
                // 路由验证，访问时，做权限得鉴定
                if (routeItem.auth && !authed) {
                  return <Redirect to="/login" />
                }
                return routeItem.render ? (
                  routeItem.render({ ...props, ...extraProps, route: routeItem })
                ) : (
                  routeItem.component && <routeItem.component {...props} {...extraProps} route={routeItem}></routeItem.component>
                )
              }
            }
          ></Route>
        ))
      }
    </Switch>
  ) : null;
}

// route/index.js
const routes = [
  { path: '/login', exact: true, icon: 'mail', component: Login },
  { path: '/404', icon: 'mail', component: NotFound },
  {
    path: '/goods', component: GoodsLayout,
    icon: 'mail',
    routes: [
      { path: '/goods/list', icon: 'mail', component: GoodsList }
    ]
  }
]